package com.gasolinator.a.app.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;
import com.gasolinator.a.R;
import com.gasolinator.a.app.GasolinatorData;
import com.gasolinator.a.app.adapter.GasolinatorStationAdapter;
import com.gasolinator.a.app.adapter.SpinnerViewSimpleBinder;
import com.gasolinator.a.app.helper.GasolinatorPreferences;
import com.gasolinator.a.augmented_reality.custom.ListActivitySensors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GasolinatorStationListActivity extends ListActivitySensors
{
    GasolinatorStationAdapter adapter;
    int type = 0;
    private ListView listView;
    private ImageButton imageButton;
    private Spinner spinner_brand;

    public void onBackAction(View paramView)
    {
        finish();
    }

    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        requestWindowFeature(1);
        requestWindowFeature(2);
        WindowManager windowManager = getWindowManager();
        if (windowManager == null) {
            setContentView(R.layout.gasolinator_list);
        } else {
            int orientation = windowManager.getDefaultDisplay().getOrientation();
            if ((orientation == 1) || (orientation == 3))
            {
                setContentView(R.layout.gasolinator_list_h);
            } else {
                setContentView(R.layout.gasolinator_list);
            }
        }

        listView = getListView();

        imageButton = (ImageButton)findViewById(R.id.topbuttonbar);

        spinner_brand = (Spinner)findViewById(R.id.spinner_brand);
        String[] from = new String[]{"_id", "name"};
        int[] to = new int[]{R.id.BrandLogoSpinner, R.id.TextSpinner};

        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        String[] choferas = getResources().getStringArray(R.array.choferas);
        for (int i = 0; i < choferas.length; i++) {
            data.add(addData(choferas[i]));
        }

        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.spinner_entry,from,to);
        adapter.setViewBinder(new SpinnerViewSimpleBinder());
        spinner_brand.setAdapter(adapter);

        Intent intent = getIntent();
        type = (intent == null) ? 0 : intent.getExtras().getInt("type", 0);
        updateListView(type);

    }

    private Spinner getSpinnerBrand()
    {


        return spinner_brand;


    }

    private Map<String, String> addData(String name) {
        Map<String, String> mapList = new HashMap<String, String>();
        mapList.put("_id", "");
        mapList.put("name", name);
        return mapList;
    }

    private void updateListView(final int type) {
        TextView titleText = (TextView)findViewById(R.id.Title);
        Drawable img;
        int destType = GasolinatorData.FAVORITES;
        switch(type){
            case 0 :
                titleText.setText(R.string.llista_preu);
                img = getResources().getDrawable(R.drawable.db_perdistancia);
                destType = GasolinatorData.DISTANCE;
                break;
            case 1 :
                titleText.setText(R.string.llista_distancia);
                img = getResources().getDrawable(R.drawable.db_perpreu);
                destType = GasolinatorData.PRICE;
                break;
            default:
                titleText.setText(R.string.llista_favorits);
                img = getResources().getDrawable(R.drawable.action_bar_preferences);
        }

        imageButton.setImageDrawable(img);

        final int finalDestType = destType;
        final GasolinatorStationListActivity activity = this;

        imageButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (finalDestType!=GasolinatorData.FAVORITES) {
                    updateListView(finalDestType);
                } else {
                    Intent intent = new Intent(activity, GasolinatorPreferencesActivity.class);
                    startActivity(intent);
                }
            }
        });

        adapter = new GasolinatorStationAdapter(this, type);
        setListAdapter(adapter);
    }



    protected void onListItemClick(ListView paramListView, View paramView, int paramInt, long paramLong)
    {
        super.onListItemClick(paramListView, paramView, paramInt, paramLong);
        Intent intent = new Intent(this, GasolinatorStationDetailActivity.class);
        intent.putExtra("fMap", false);
        intent.putExtra("selected", paramInt);
        startActivity(intent);
    }

    protected void onPause()
    {
        super.onPause();
    }

    public void onGasolinatorPreferencesAction(View paramView)
    {

    }

    protected void onResume()
    {
        super.onResume();
        GasolinatorPreferences gasolinatorPreferences = new GasolinatorPreferences(getApplicationContext());
        String combustible = gasolinatorPreferences.getCombustible();
        adapter.setCombustible(combustible);
        adapter.notifyDataSetChanged();

        Boolean fullscreen = gasolinatorPreferences.getFullscreen();
        if (fullscreen) {
            getWindow().setFlags(1024, 1024);
        }  else
        {
            getWindow().setFlags(0, 1024);
        }
        if (type == 2 && listView.getCount()==0) {
            Toast localToast = Toast.makeText(getApplicationContext(), R.string.info_add_favorites, 1);
            localToast.setGravity(17, 0, 0);
            localToast.show();
        }


    }
}